package com.nantai.jiafang.nantaijiafang.deviceManager.devicemanager;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.deviceManager.Adapter.TaskDetailAdapter;
import com.nantai.jiafang.nantaijiafang.deviceManager.bean.TaskDetail;
import com.nantai.jiafang.nantaijiafang.deviceManager.constant.Constans;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.Error;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.HttpClient;
import com.nantai.jiafang.nantaijiafang.deviceManager.http.ResponseHandler;
import com.nantai.jiafang.nantaijiafang.deviceManager.utils.MessagePop;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import okhttp3.Response;
import okhttp3.ResponseBody;
import retrofit2.Call;


public class TaskDetailActivity extends BaseActivity {

    @InjectView(R.id.tv_toolbar_title)
    TextView mTvToolbarTitle;
    @InjectView(R.id.toolbar)
    Toolbar mToolbar;
    @InjectView(R.id.name_tv)
    TextView mNameTv;
    @InjectView(R.id.begintime_tv)
    TextView mBegintimeTv;
    @InjectView(R.id.list)
    ListView mList;
    @InjectView(R.id.send_btn)
    Button mSendBtn;
    @InjectView(R.id.check_btn)
    Button mCheckBtn;
    private MaterialDialog showDialog;
    List mDataSource = null;
    String mTaskid;
    String mPlaceId;
    String mDeviceCount;
    String mBeginTime;
    String mName;
    boolean isTask;
    TaskDetailAdapter mTaskDetailAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_task_detail);
        ButterKnife.inject(this);
        mTvToolbarTitle.setText("任务详细");
        mToolbar.setNavigationIcon(R.drawable.md_nav_back);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mPlaceId = this.getIntent().getStringExtra("id");
        mBeginTime = this.getIntent().getStringExtra("beginTime");
        mName = this.getIntent().getStringExtra("name");
    }

    @Override
    protected void onResume() {
        super.onResume();
        showDialog = new MaterialDialog.Builder(TaskDetailActivity.this)
                .content("正在请求数据")
                .progress(true, 0, false)
                .cancelable(false).build();
        requestTaskDetail();
    }

    private void updateList(TaskDetail taskDetail) {

        mNameTv.setText("库位:"+mName);
        isTask= taskDetail.getIs_task()==1;
        if (taskDetail.getIs_task() == 1) {
            mBegintimeTv.setText("开始盘点时间:"+mBeginTime);
            mTaskid = taskDetail.getTask_id();
            mList.setVisibility(View.VISIBLE);
            mDataSource = new ArrayList();
            mDataSource.addAll(taskDetail.getDetail());
            mTaskDetailAdapter = new TaskDetailAdapter(mDataSource, this);
            mList.setAdapter(mTaskDetailAdapter);
            mSendBtn.setText("结束盘点");
            mCheckBtn.setVisibility(View.VISIBLE);
        } else {
            mBegintimeTv.setVisibility(View.GONE);
            mList.setVisibility(View.GONE);
            mSendBtn.setText("开始盘点");
            mCheckBtn.setVisibility(View.GONE);
        }

    }

    private void requestTaskDetail() {
        showDialog.show();
        HttpClient client = new HttpClient(TaskDetailActivity.this);
        Call call = client.service().taskDetail(mPlaceId);
        client.request(call, new ResponseHandler<TaskDetail>() {
            @Override
            public void onSuccess(TaskDetail taskDetail) {
                showDialog.dismiss();
                updateList(taskDetail);
            }

            @Override
            public void onFailure(int code, Error e) {
                showDialog.dismiss();
                MaterialDialog logoutDialog = new MaterialDialog.Builder(TaskDetailActivity.this)
                        .title("请求数据失败").negativeText("返回").positiveText("重新请求").onPositive(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                requestTaskDetail();
                            }
                        }).onNegative(new MaterialDialog.SingleButtonCallback() {
                            @Override
                            public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                TaskDetailActivity.this.finish();
                            }
                        }).cancelable(false).show();
            }
        });
    }

    @OnClick({R.id.send_btn, R.id.check_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.send_btn:
            {
                showDialog.show();
                if(isTask)
                {
                    HttpClient client = new HttpClient(TaskDetailActivity.this);
                    Call call = client.service().finishTask(mTaskid);
                    client.request(call, new ResponseHandler<ResponseBody>() {
                        @Override
                        public void onSuccess(ResponseBody res) {
                            showDialog.dismiss();
                            MessagePop.ToastMessage(TaskDetailActivity.this, "结束盘点");
                            finish();
                        }
                        @Override
                        public void onFailure(int code, Error e) {
                            showDialog.dismiss();
                        }
                    });
                }else
                {
                    HttpClient client = new HttpClient(TaskDetailActivity.this);
                    Call call = client.service().beginTask(mPlaceId);
                    client.request(call, new ResponseHandler<Map<String, String>>() {
                        @Override
                        public void onSuccess(Map<String, String> res) {
                            mTaskid = res.get("id");
                            showDialog.dismiss();
                            MessagePop.ToastMessage(TaskDetailActivity.this, "开始盘点");
                            mCheckBtn.setVisibility(View.VISIBLE);
                            mSendBtn.setText("结束盘点");

                            Intent intent = new Intent(TaskDetailActivity.this, CaptureActivity.class);
                            intent.putExtra("operate_type", String.valueOf(Constans.TaskCode));
                            intent.putExtra("taskid", String.valueOf(mTaskid));
                            intent.putExtra("placeid", String.valueOf(mPlaceId));
                            startActivity(intent);
                        }
                        @Override
                        public void onFailure(int code, Error e) {
                            showDialog.dismiss();
                        }
                    });
                }
            }
                break;
            case R.id.check_btn:
            {
                Intent intent = new Intent(TaskDetailActivity.this, CaptureActivity.class);
                intent.putExtra("operate_type", String.valueOf(Constans.TaskCode));
                intent.putExtra("taskid", String.valueOf(mTaskid));
                intent.putExtra("placeid", String.valueOf(mPlaceId));
                startActivity(intent);
            }
                break;
        }
    }
}
